<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!--360浏览器优先以webkit内核解析-->
    <title>wms</title>
    <link rel="shortcut icon" href="favicon.ico">
    <link href="../static/css/bootstrap.min.css" th:href="@{/css/bootstrap.min.css}" rel="stylesheet"/>
    <link href="../static/css/font-awesome.min.css" th:href="@{/css/font-awesome.min.css}" rel="stylesheet"/>
    <link href="../static/css/style.min.css" th:href="@{/css/style.min.css}" rel="stylesheet"/>
    <link href="../static/page/css/index.css" th:href="@{/page/css/index.css}" rel="stylesheet"/>
    <style type="text/css">
        .gray-bg {
            background-color: #fff;
        }
    </style>
</head>

<body class="gray-bg">
<div class="row white-bg dashboard-header">
    <div class="col-md-3">
        <a th:onclick="funClick(this,[[${clientId}]],'entry-large',false)" class="thumbnail">
            <img th:src="@{/page/img/houseWare.jpg}" alt="采购入库" style="max-height: 180px;max-width: 170px">
        </a>
        <div class="caption" style="text-align: center;">
            <h3>采购入库</h3>
        </div>
        <br>
    </div>
    <div class="col-sm-3">
        <a th:onclick="funClick(this,[[${clientId}]],'large',true)" class="thumbnail">
            <img th:src="@{/page/img/itemPick.jpg}" alt="大件分拣" style="max-height: 180px;max-width: 170px">
        </a>
        <div class="caption" style="text-align: center;">
            <h3>大件领取</h3>
        </div>
    </div>
    <div class="col-sm-3">
        <a th:onclick="funClick(this,[[${clientId}]],'return-large',true)" class="thumbnail">
            <img th:src="@{/page/img/warhouse.jpg}" alt="退货入库" style="max-height: 180px;max-width: 170px">
        </a>
        <div class="caption" style="text-align: center;">
            <h3>退货入库(<span id="return-count" style="color: #cc0000"></span>)</h3>
        </div>
        <br>
    </div>
    <div class="col-sm-3">
        <a th:onclick="funClick(this,[[${clientId}]],'frmloss-large',false)" class="thumbnail">
            <img th:src="@{/page/img/warhouse.jpg}" alt="临期出库" style="max-height: 180px;max-width: 170px">
        </a>
        <div class="caption" style="text-align: center;">
            <h3>临期出库(<span id="frm-loss-quantity" style="color: #cc0000"></span>)</h3>
        </div>
        <br>
    </div>
</div>

<script th:src="@{/js/jquery.min.js}"></script>
<script th:src="@{/js/bootstrap.min.js}"></script>
<script th:src="@{/ajax/libs/layer/layer.min.js}"></script>
<script type="module" th:src="@{/page/js/cookie/js.cookie-2.2.1.min.js}"></script>
<script th:inline="javascript">
    $(function () {
        $('#pay-qrcode').click(function () {
            var html = $(this).html();
            parent.layer.open({
                title: false,
                type: 1,
                closeBtn: false,
                shadeClose: true,
                area: ['600px', '360px'],
                content: html
            });
        });
        returnCount();
        frmLossCount();
    });

    /**
     * 退货统计
     */
    function returnCount() {
        const opt = {};
        $.ajax({
            type: "POST",
            url: "/wcs/sale/order/return/count",
            contentType: "application/json;charset=UTF-8",
            data: JSON.stringify(opt),
            dataType: "json",
            success: function (data) {
                if (data.code == 0) {
                    if (data.data && data.data.length > 0) {
                        let bigCount = 0;
                        let standardCount = 0;
                        for (let i = 0; i < data.data.length; i++) {
                            const saleOrderDetail = data.data[i];
                            if (saleOrderDetail.bigProduct === 1) {
                                bigCount = saleOrderDetail.quantity;
                            } else {
                                standardCount = saleOrderDetail.quantity
                            }
                        }
                        $("#return-count").text("地平仓：" + bigCount + "；货架：" + standardCount);
                    } else {
                        $("#return-count").text("地平仓：0；货架：0");
                    }

                } else {
                    console.log("退货统计:" + data.msg);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                BootstrapDialog.alert({
                    title: "信息提示！",
                    closable: false,
                    message: XMLHttpRequest.responseJSON.msg
                });
            }
        });
    }

    /**
     * 临期统计
     */
    let count = 0;

    function frmLossCount() {
        const opt = {};
        const model = {};
        model['status'] = 1;
        model['type'] = 1;
        model['treated'] = false;
        model['dataType'] = "HSCK";
        opt['data'] = model;
        $.ajax({
            type: "POST",
            url: "/wcs/record/untreated/count",
            contentType: "application/json;charset=UTF-8",
            data: JSON.stringify(opt),
            dataType: "json",
            success: function (data) {
                if (data.code == 0) {
                    if (data.data && data.data.length > 0) {
                        let bigCount = 0;
                        let standardCount = 0;
                        for (let i = 0; i < data.data.length; i++) {
                            const spuSortRecord = data.data[i];
                            if (spuSortRecord.bigProduct === 1) {
                                bigCount = spuSortRecord.quantity;
                                count = spuSortRecord.quantity;
                            } else {
                                standardCount = spuSortRecord.quantity
                            }
                        }
                        $("#frm-loss-quantity").text("地平仓：" + bigCount + "；货架：" + standardCount);
                    } else {
                        $("#frm-loss-quantity").text("地平仓：0；货架：0");
                    }
                } else {
                    console.log("临期统计:" + data.msg);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                BootstrapDialog.alert({
                    title: "信息提示！",
                    closable: false,
                    message: XMLHttpRequest.responseJSON.msg
                });
            }
        });
    }

    /**
     * 页面跳转
     *
     * @param obj
     * @param clientId
     * @param page
     * @param flag
     */
    function funClick(obj, clientId, page, flag) {
        const url = '/mqtt/index/test?clientId=' + clientId + '&page=' + page;
        console.log("clientId:" + clientId + ";page=" + page);
        if (flag) {
            checkUntreatedCount(url);
        } else if (page === 'entry') {
            checkPurchaseEntry(obj, clientId, page, flag, url);
        } else {
            window.location.href = url;
        }

    }

    /**
     * 检查是否有相同入库单
     *
     * @param obj
     * @param clientId
     * @param page
     * @param flag
     * @param url
     */
    function checkPurchaseEntry(obj, clientId, page, flag, url) {
        const opt = {};
        const model = {};
        model['clientId'] = clientId;
        opt['upOrDownPolicy'] = model;
        opt['bigProduct'] = 1;
        $.ajax({
            type: "POST",
            url: "/wcs/order/entry/checkPurchaseEntry",
            contentType: "application/json;charset=UTF-8",
            data: JSON.stringify(opt),
            dataType: "json",
            success: function (data) {
                if (data.code == 0) {
                    window.location.href = url;
                } else {
                    if (data.data) {
                        layer.alert("存在采购入库单据" + data.data + "，请处理完再进入采购入库！");
                    } else {
                        layer.alert(data.msg);
                    }
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                BootstrapDialog.alert({
                    title: "信息提示！",
                    closable: false,
                    message: XMLHttpRequest.responseJSON.msg
                });
            }
        });
    }

    function checkUntreatedCount(url) {
        frmLossCount();
        if (count != 0) {
            layer.alert("存在临期出库商品，请先处理临期出库商品！");
            // window.location.href = url;
        } else {
            window.location.href = url;
        }
    }
</script>
</body>
</html>
